package com.billy.ohos.loadingstatusview.wrapcomponent;

import com.billy.ohos.loading.Gloading;
import com.billy.ohos.loadingstatusview.ResourceTable;
import com.billy.ohos.loadingstatusview.util.Util;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.element.Element;

/**
 * demo:
 *      bind status view to multiple specific views
 * @author billy.qi
 * @since 19/3/19 23:20
 */
public class WrapScrollAbility extends Ability {

    private DirectionalLayout container;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_wrap_scroll);
        container = (DirectionalLayout) findComponentById(ResourceTable.Id_container);
        addImages();
    }

    private void addImages() {
        addImage(Util.getErrorImage());
        addImage(Util.getRandomImage1());
        addImage(Util.getErrorImage());
        addImage(Util.getRandomImage2());
        addImage(Util.getRandomImage1());
        addImage(Util.getRandomImage2());
        addImage(Util.getRandomImage1());
        addImage(Util.getRandomImage2());
        addImage(Util.getRandomImage1());
        addImage(Util.getRandomImage2());
    }

    private void addImage(final String url) {
        final Image image = new Image(this);
        DirectionalLayout.LayoutConfig lg = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT, 300);
        container.addComponent(image, lg);
        final Gloading.Holder holder = Gloading.getDefault().wrap(image);
        holder.withRetry(new Runnable() {
            @Override
            public void run() {
                loadImage(holder, image, url);
            }
        });
        loadImage(holder, image, url);
    }

    private void loadImage(final Gloading.Holder holder, final Image img, String url) {
        holder.showLoading();
        getUITaskDispatcher().delayDispatch(new Runnable() {
            @Override
            public void run() {
                Glide.with(WrapScrollAbility.this)
                        .load(url)
                        .listener(new RequestListener<Element>() {
                            @Override
                            public boolean onLoadFailed(GlideException exception, Object obj, Target<Element> target, boolean boo) {
                                holder.showLoadFailed();
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(Element element, Object obj, Target<Element> target, DataSource dataSource, boolean boo) {
                                holder.showLoadSuccess();
                                return false;
                            }
                        })
                        .into(img);
            }
        }, 50);
    }

}
